package tereshchuk;

/**
 *
 * @author Pasha
 */
import tereshchuk.CalcBase;
import java.util.Scanner;

public class PrintConsole {

    private Scanner keyboard = new Scanner(System.in);
    private double val1 = 0;
    private double val2 = 0;
    private boolean isEnterValueFromConsole = true;
    private String mathematicalOperations = "";

    private void enterValue1(String Val) {
        do {
            System.out.print(Val);
            if (keyboard.hasNextInt()) {
                this.val1 = (double) keyboard.nextInt();
                break;
            } else {
                if (keyboard.hasNextDouble()) {
                    this.val1 = keyboard.nextDouble();
                    break;
                } else {
                    System.out.println("\tToken not an integer or a real value.");
                    keyboard.next();
                }
            }
        } while (true);
    }

    private void enterValue2(String Val) {
        do {
            System.out.print(Val);
            if (keyboard.hasNextInt()) {
                this.val2 = (double) keyboard.nextInt();
                break;
            } else {
                if (keyboard.hasNextDouble()) {
                    this.val2 = Double.valueOf(keyboard.nextDouble());
                    break;
                } else {
                    System.out.println("\tToken not an integer or a real value.");
                    keyboard.next();
                }
            }
        } while (true);
    }

    private void outValue(double Val) {
        if (Val % 1 == 0) {
            System.out.println("Result: " + (int) Val);
        } else {
            System.out.println("Result: " + Val);
        }
    }

    private void printMathematicalOperations() {
        System.out.println("Operations:");
        for (MathematicalOperations e : MathematicalOperations.values()) {
            System.out.println(Integer.toString(e.ordinal()) + ": " + e.toString());
        }
        System.out.println("Select operations:");
    }

    private boolean checkArg(String[] arg) {
        if (arg.length == 0) {
            return true;
        }
        if (arg.length < 3) {
            System.out.println("Not enough parameters");
            return false;
        }
        if ( (!arg[0].matches("[-]*\\d+([.||,]?\\d+)?")) || 
                (!arg[1].matches("[-]*\\d+([.||,]?\\d+)?")) ) {
            System.out.println("One parameter is not numeric");
            return false;
        }
        for (MathematicalOperations opp: MathematicalOperations.values()) {
            if ( opp.name().equals(arg[2].toUpperCase()) ){
                isEnterValueFromConsole = false;
                mathematicalOperations = opp.name();
                val1 = Double.valueOf(arg[0].replace(',', '.'));
                val2 = Double.valueOf(arg[1].replace(',', '.'));
                return true;
            }
        }
        return false;    
    }

public void print(String[] arg) {
        if (!checkArg(arg)) {
            return;
        }
        CalcBase calcBase = new CalcBase();
        int selOpp;

        System.out.println("Runing Calc");
        if (isEnterValueFromConsole) {
            enterValue1("Enter first value:");
            enterValue2("Enter second value:");
            printMathematicalOperations();
            do {
                if (keyboard.hasNextInt()) {
                    selOpp = keyboard.nextInt();
                    for (MathematicalOperations e : MathematicalOperations.values()) {
                        if (e.ordinal() == selOpp) {
                            mathematicalOperations = e.name();
                        }
                    }

                    break;
                } else {
                    System.out.println("Value is not digital! Select again.");
                    keyboard.next();
                }
            } while (true);
        }
        outValue(calcBase.execOperation(MathematicalOperations.valueOf(mathematicalOperations), val1, val2));
        System.out.println("The End!");
    }

}
